package com.jbacon.everadiodownloader.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class CLICommand {
    public String run(final String commandToRun) {
        final StringBuilder programResults = new StringBuilder();
        try {
            final Process process = Runtime.getRuntime().exec(commandToRun);
            final BufferedReader processOutput = new BufferedReader(new InputStreamReader(process.getInputStream()));
            getProcessOutput(programResults, processOutput);

        } catch (final IOException e) {
            System.err.println("A problem occured when running: " + commandToRun);
        }

        return programResults.toString();
    }


    public String runQuiet(final String commandToRun) {
        final StringBuilder programResults = new StringBuilder();
        try {
            final Process process = Runtime.getRuntime().exec(commandToRun);
            final BufferedReader processOutput = new BufferedReader(new InputStreamReader(process.getInputStream()));
            getProcessOutput(programResults, processOutput);

        } catch (final IOException e) {
        }

        return programResults.toString();
    }


    private void getProcessOutput(final StringBuilder processOutput, final BufferedReader processOutputStream)
            throws IOException {
        String s = null;
        while ((s = processOutputStream.readLine()) != null) {
            processOutput.append(s);
        }
    }
}
